package javango.forms;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.inject.Inject;
import com.google.inject.Injector;

import javango.forms.fields.FieldFactory;

public class FormsImpl implements Forms {

	private final static Log log = LogFactory.getLog(FormsImpl.class);
	
	protected FieldFactory fieldFactory;
	protected Injector injector;
	
	@Inject
	public FormsImpl(FieldFactory fieldFactory, Injector injector) {
		super();
		this.fieldFactory = fieldFactory;
		this.injector = injector;
	}
	
	public InlineForms inlineForms(String prefix) {
		return new InlineForms(injector, prefix);
	}
	public InlineForms inlineForms(String prefix, String coreField, Class<? extends Form> formClass, Map<String, String[]> values) {
		return new InlineForms(injector, prefix, coreField, formClass, values);
	}
	
	/**
	 * Create a new ModelForm   
	 */
	public Form forInstance(Object instance) {	
		ModelForm form = new ModelForm(fieldFactory).setInitial(instance);
		return form;
	}
	
	public Form forModel(Class modelClass) {		
		ModelForm form = new ModelForm(fieldFactory).setModel(modelClass);
		return form;
	}
	
	public <T extends Form> T newForm(Class<T> formClass) {
		return injector.getInstance(formClass);
	}

}
